// pages/iteminfo/iteminfo.js
var QQMapWX = require('../../qqmap-wx-jssdk.min.js');
 
// 实例化API核心类
var qqmapsdk = new QQMapWX({
    key: '6FTBZ-OPECR-OYVWT-WULAV-V7VPQ-FAFCT' // 必填
});

Page({
  data: {

    itemid:"",

    itempic:[],
    title:'',
    value:false,
    lease:false,
    times:'',
    deposit:'',

    // 交易方式
    way1:false,
    way2:false,
    way3:false,

    rentstate:"",

    moreinfo:true,
    rentprocess:'none',
    damage:'none',

    info:true,
    process:false,
    dam:false,

    judgenum:"",

    //是否收藏
    collect:'',
    
    itemdetail:"",
    address:'',
    //个人信息
    personaltarget:'none',
    ava:"",
    nickname:"",
    username:"",
    profession:"",
    class:"",
    phone:"",
    wxnumber:"",
    qqnumber:"",

    // 是否可以购买
    canrent:true
  },

  onLoad(options){
    var that = this;
    // console.log(options.itemid)
    var itemid = options.itemid;
    this.setData({
      itemid:itemid
    })

    //渲染商品信息
    
    wx.request({
      url: 'http://127.0.0.1:8080/item/selectItemByItemId',
      method:"POST",
      data:{
        itemid:itemid
      },
      success(res){
        // console.log(res)
        if(res.data.length != 0 ){

          console.log(res.data[0])
          var iteminfo = res.data[0];
          iteminfo.itemPic = JSON.parse(iteminfo.itemPic);
          // if(){
          //   that.setData({
              
          //   })
          // }

          if(iteminfo.userID == wx.getStorageSync('openid') || iteminfo.rentState == "租用中"){
            that.setData({
              canrent:false
            })
          }

          // if(iteminfo.)
          // console.log(iteminfo.rentWay.length)
          // 渲染是否可以续租
          if(iteminfo.isRent == "是"){
            that.setData({
              lease:true
            })
          }

          // 渲染交易方式
          for(var i = 0;i<iteminfo.rentWay.length;i++){
            if(iteminfo.rentWay[i] == 0 ){
              that.setData({
                way1:true
              })
            }
            if(iteminfo.rentWay[i] == 1){
              that.setData({
                way2:true
              })
            }
              if(iteminfo.rentWay[i] == 2){
                that.setData({
                  way3:true
                })
            }
          
          }

          // 渲染全部数据
          that.setData({
            itempic:iteminfo.itemPic,
            title:iteminfo.itemTitle,
            value:iteminfo.dayRent,
            times:iteminfo.rentTimes,
            deposit:iteminfo.deposit,
            rentstate:iteminfo.rentState,
            seenum:iteminfo.seeNum,
            submittime:iteminfo.submitDate,
            itemdetail:iteminfo.itemInfo,
            address:iteminfo.sendAddress,
            ava:iteminfo.wxAva,
            nickname:iteminfo.nickName,
            username:iteminfo.userName,
            profession:iteminfo.profession,
            class:iteminfo.userClass,
            phone:iteminfo.contactPhone,
            wxnumber:iteminfo.contactWx,
            qqnumber:iteminfo.contactQq,
          })

          wx.request({
            url: 'http://127.0.0.1:8080/item/updateSeeNum',
            method:"POST",
            data:{itemid:itemid},
            success(res){
              // console.log(res)
              if(res.data){
                that.setData({
                  seenum:that.data.seenum+1
                })
              }
            }
          })
        }
      }
    })

    //渲染评论数
    wx.request({
      url: 'http://127.0.0.1:8080/itemJudge/selectJudgeByItemid',
      method:"POST",
      data:{itemid:itemid},
      success(res){
        that.setData({
          judgenum:res.data.length
        })
        // console.log(res)
      }
    })

    //是否收藏
    wx.request({
      url: 'http://127.0.0.1:8080/itemCollect/itemCollect',
      method:"POST",
      data:{
        itemid:options.itemid,
        userid:wx.getStorageSync('openid')
      },
      success(res){
        if(res.data.length != 0){
          that.setData({
            collect:true
          })
        }else{
          that.setData({
            collect:false
          })
        }
        console.log(res)
      }
    })

  },

// 查看评论
  lookjudge(){
    wx.navigateTo({
      url: '/pages/judgecontent/judgecontent?itemid='+this.data.itemid,
    })
  },
  chooseinfo(e){
    this.setData({
    moreinfo:true,
    rentprocess:'none',
    damage:'none',
    info:true,
    process:false,
    dam:false
    })
  },
  chooseprocess(e){
    this.setData({
    moreinfo:'none',
    rentprocess:true,
    damage:'none',
    info:false,
    process:true,
    dam:false
    })
  },
  choosedamage(e){
    this.setData({
    moreinfo:'none',
    rentprocess:'none',
    damage:true,
    info:false,
    process:false,
    dam:true
    })
  },

  call(){
    wx.makePhoneCall({
      phoneNumber: '1111111111',
    })
  },
  //收藏物品信息
  collect(e){
    if(wx.getStorageSync('openid') != ""){
      var that = this;
      var value = this.data.collect
      that.setData({
        collect:!value
      })
     console.log(value)
      if(!value){
        var nowtime = new Date().toJSON().substring(0,10)+' '+new Date().toTimeString().substring(0,8);
        var data = {
          itemcollecttime:nowtime,
          useritemcollect:wx.getStorageSync('openid'),
          itemid:that.data.itemid,
        }
        // console.log(data)
        wx.request({
          url: 'http://127.0.0.1:8080/itemCollect/addItemCollect',
          method:"POST",
          data:data,
          success(res){
            if(res.data){
              wx.showToast({
                title: '收藏成功',
                duration: 2000,
                icon: "none",
                success: (res) => {},
              })
            }
            console.log(res)
          }
        })
      }else{
        var data = {
          userid:wx.getStorageSync('openid'),
          itemid:that.data.itemid,
        }
        // console.log(data)
        wx.request({
          url: 'http://127.0.0.1:8080/itemCollect/delItemCollect',
          method:"POST",
          data:data,
          success(res){
            // console.log(res)
          }
        })
      }
    }else{
      wx.showToast({
        title: '请先登录',
        icon:"error"
      })
    }

  },
  //展示地址位置
  showaddress(e){
    var _this = this;

    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: _this.data.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'

      success: function(res) {//成功后的回调
        console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        
        console.log(latitude,longitude)

        //根据地址解析在地图上标记解析地址位置
        _this.setData({ // 获取返回结果，放到markers及poi中，并在地图展示
          markers: [{
            id: 0,
            title: res.title,
            latitude: latitude,
            longitude: longitude,
            iconPath: '/pic/地标.png',//图标路径
            width: 30,
            height: 30,
            // callout: { //可根据需求是否展示经纬度
            //   content: latitude + ',' + longitude,
            //   color: '#000',
            //   display: 'ALWAYS'
            // }
          }],
          poi: { 
            latitude: latitude,
            longitude: longitude
          },
        });

        wx.openLocation({
          latitude:latitude,
          longitude:longitude,
          address: _this.data.address,
          name: _this.data.address,
          scale: 28,
        })
      },
      fail: function(error) {
        console.error(error);
      },
      complete: function(res) {
        console.log(res);
      }
    })
  },

  //展示用户信息
  showperson(e){
    if(wx.getStorageSync('isuser') == "yes"){
       // console.log(e)
    var index = e.currentTarget.dataset.index;
    this.setData({
      personaltarget:true
    })
    }else{
       wx.showToast({
      title: '身份未通过审核',
      duration: 1000,
      icon: "error",
      success: (res) => {},
    })
    }
   
   
  },
  cancelperson(e){
    this.setData({
      personaltarget:'none'
    })
  },

  //租用物品
  rent(){
    if(wx.getStorageSync('openid') == ""){
      wx.showToast({
        title: '请先登录',
        icon:"error"
      })
    }else if(wx.getStorageSync('isuser') != "yes"){
      wx.showToast({
        title: '用户身份未验证',
        icon:"error"
      })
    }else if(wx.getStorageSync('identity') != "学生用户"){
      wx.showToast({
        title: '您不是学生用户',
        icon:"error"
      })
    }else{
      if(this.data.canrent){
        var itemid = this.data.itemid;
        wx.navigateTo({
          url: '/pages/rent/rent?itemid='+itemid,
        })
      }else{
        wx.showToast({
          title: '不能租赁自己的物品',
          icon:"none"
        })
      }
    }

   
  }
  
})